package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.SpuInfoEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> wrapper = new LambdaQueryWrapper<>();
        String key = (String)params.get("key");
        String catalogId = (String)params.get("catelogId");
        String brandId = (String)params.get("brandId");
        String min = (String)params.get("min");
        String max = (String)params.get("max");
        if(StringUtils.isNotBlank(key)){
            wrapper.and(w->{
                w.like(SkuInfoEntity::getSkuName,params.get("key"))
                        .or().eq(SkuInfoEntity::getSkuId,params.get("key"));
            });
        }
        if(StringUtils.isNotBlank(catalogId)&&!"0".equals(catalogId)){
            wrapper.eq(SkuInfoEntity::getCatalogId,catalogId);
        }

        if(StringUtils.isNotBlank(brandId)&&!"0".equals(brandId)){
            wrapper.eq(SkuInfoEntity::getBrandId,brandId);
        }
        if(StringUtils.isNotBlank(min)&&!"0".equals(min)){
            wrapper.ge(SkuInfoEntity::getPrice,min);
        }
        if(StringUtils.isNotBlank(max)&&!"0".equals(max)){
            wrapper.le(SkuInfoEntity::getPrice,max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> list = this.list(new LambdaQueryWrapper<SkuInfoEntity>().eq(SkuInfoEntity::getSpuId, spuId));
        return list;
    }

}